<?php
#过期品牌账户

use Biz\model\brandAccount\BrandAccountInfo;

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

$brandId = $argv[1] ?? 0;

$startTime = $argv[2] ?? null;

$endTime = $argv[3] ?? null;

if (!($brandId && $startTime && $endTime)) {
    echo "参数错误" . PHP_EOL;
    exit;
}

$startTimeStamp = strtotime($startTime);

$endTimeStamp = strtotime(date('Y-m-d 23:59:59', strtotime($endTime)));

var_dump($startTimeStamp,$endTimeStamp);

$accountInfo = BrandAccountInfo::getOneByWhere([
    'type' => BrandAccountInfo::ACCOUNT_TYPE_BRAND,
    'brand_id' => $brandId,
    'account_status' => BrandAccountInfo::ACCOUNT_STATUS_USED
], ['id', 'in_start_time', 'in_end_time']);

if (!$accountInfo) {
    echo "当前品牌账户状态异常" . PHP_EOL;
    exit;
}

$orderInfo = \Biz\model\brand\BrandOrder::get('*',
    [
        'brand_id' => $brandId,
        'order_status' => \Biz\model\brand\BrandOrder::ORDER_STATUS_FINISHED,
        'pay_status' => \Biz\model\brand\BrandOrder::PAY_STATUS_PAID,
        'service_status' => \Biz\model\brand\BrandOrder::SERVICE_STATUS_FINISHED,
        'ORDER' => ['id' => 'DESC'],
    ]
);

BrandAccountInfo::updateData(['in_start_time' => $startTime, 'in_end_time' => $endTime], ['id' => $accountInfo['id']]);

var_dump('BrandAccountInfo',BrandAccountInfo::log());

\Biz\model\brand\BrandOrder::update([
    'in_start_time' => $startTime,
    'in_end_time' => $endTime,
    'service_status' => \Biz\model\brand\BrandOrder::SERVICE_STATUS_FINISHED,
], ['id' => $orderInfo['id']]);

var_dump('BrandOrder',\Biz\model\brand\BrandOrder::log());

\Biz\model\brandRights\BrandPackageRights::update(
    [
        'start_time' => $startTimeStamp,
        'end_time' => $endTimeStamp
    ],
    [
        'brand_id' => $brandId,
        'rights_status' => 1
    ]
);

var_dump('BrandPackageRights',\Biz\model\brandRights\BrandPackageRights::log());

\Biz\model\brandRights\BrandRightsAll::update(
    [
        'start_time' => $startTimeStamp,
        'end_time' => $endTimeStamp
    ],
    [
        'brand_id' => $brandId,
        'rights_status' => 1
    ]
);

var_dump('BrandRightsAll',\Biz\model\brandRights\BrandRightsAll::log());

$records = \Biz\model\brandRights\BrandRightsRecored::selectList(
    [
        'brand_id' => $brandId,
        'addtime[>=]' => $accountInfo['in_start_time'],
        'addtime[<=]' => $accountInfo['in_end_time'],
    ]
);

foreach ($records as $record) {
    $upData = [];
    if (!empty($record['source_data'])) {
        $sourceData = json_decode($record['source_data'], true);
        if ($sourceData && is_array($sourceData)) {
            if (isset($sourceData['end_time']) && is_numeric($sourceData['end_time'])) {
                $sourceData['end_time'] = $endTimeStamp;
                $upData['source_data'] = json_encode($sourceData);
            }
        }
    }
    if (!empty($record['operate_data'])) {
        $opData = json_decode($record['operate_data'], true);
        if ($opData && is_array($opData)) {
            if (isset($opData['end_time']) && is_numeric($opData['end_time'])) {
                $opData['end_time'] = $endTimeStamp;
                $upData['operate_data'] = json_encode($opData);
            }
        }
    }

    if (!empty($upData)) {
        \Biz\model\brandRights\BrandRightsRecored::update($upData, ['id' => $record['id']]);
        var_dump('BrandRightsRecored',\Biz\model\brandRights\BrandRightsRecored::log());
    }

}

echo "barnd expired".PHP_EOL;

require_once '/data/wwwroot/g_biz/cli/Crond/BrandAccount/checkBrand.php';







